//
//  MessageFrame.m
//  3 -- QQ project
//
//  Created by Rainy Zou on 15/11/24.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "MessageFrame.h"

#define MARGIN 10
#define CELL_WIDTH 375
#define MAX_TEXT_WIDTH 200

@implementation MessageFrame

- (void)setMessage:(JSMessage *)message
{
    _message = message;
    
    //计算Frame
    CGFloat margin = MARGIN;
    
    if(message.isHiddenTime){
        _timerF = CGRectMake(0, 0, 0, 0);
    }else{
    //时间
    CGFloat timerX =MARGIN;
    CGFloat timerY = MARGIN;
    CGFloat timerW = CELL_WIDTH - 2*MARGIN;
    CGFloat timerH = 40;
    
    _timerF = CGRectMake(timerX, timerY, timerW, timerH);
    }
    //头像
    CGFloat iconH = 50;
    CGFloat iconW = 50;
    CGFloat iconY = CGRectGetMaxY(self.timerF)+MARGIN;
    CGFloat iconX;
    if(self.message.type == JSMessageTypeMe){
        iconX = CELL_WIDTH - iconW - MARGIN;
    }else{
        iconX = MARGIN;
    }
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);

    //消息
    CGSize textMaxSize = CGSizeMake(MAX_TEXT_WIDTH, MAXFLOAT);
    CGSize textSize = [self sizeWithText:self.message.text font:[UIFont systemFontOfSize:15] maxSize:textMaxSize];
    CGSize buttonSize = CGSizeMake(textSize.width +40, textSize.height +40);
    
    CGFloat textX = 0;
    CGFloat textY = iconY;
    if(self.message.type == JSMessageTypeMe)
    {
        textX = iconX - buttonSize.width -MARGIN;
    }else{
        textX = CGRectGetMaxX(self.iconF)+MARGIN;
    }
    
    _textF = CGRectMake(textX, textY, buttonSize.width, buttonSize.height);
    
    CGFloat textMaxH = CGRectGetMaxY(self.textF);
    CGFloat iconMaxH = CGRectGetMaxY(self.iconF);
    
    _rowHeight = MAX(textMaxH,iconMaxH)+MARGIN;
    

}

-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}

@end
